package com.example.myworkers.service.imp;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myworkers.dao.GrantMapper;
import com.example.myworkers.model.Car;
import com.example.myworkers.model.Goods;
import com.example.myworkers.service.GoodsService;
import com.example.myworkers.dao.GoodsMapper;
import com.example.myworkers.service.GrantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 门硕然
* @description 针对表【goods】的数据库操作Service实现
* @createDate 2023-10-27 09:42:08
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService{

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GrantMapper grantMapper;
    //添加
    @Override
    public int insertGoods(Goods goods) {
        int value=0;
    //商品名称不能重复
        List<Goods> getG=goodsMapper.selectAllByName(goods.getName());
        if (getG.size()>=1){
            value=-1;
        } else {
            //商品id不为0
            if(goods.getGoodsid()!=0){
                //价格price为正数
                if(goods.getPrice()>=0){
                    value=goodsMapper.insertSelective(goods);
                }
            }
        }
        return value;
    }

    @Override
    public Goods selectGoods(int id) {
        Goods goods1=new Goods();
        if (id!=0){
            goods1=goodsMapper.selectOneByGoodsid(id);
        }
        return goods1;
    }

    @Override
    public int updateGoods(Goods goods) {

        int value=0;
        //主键不为0
        if(goods.getGoodsid()!=0) {
            //主键信息必须存在
            if(goodsMapper.selectOneByGoodsid(goods.getGoodsid())!=null){
                    value=goodsMapper.updateSelective(goods);
            }
        }
        return value;

    }

    @Override
    public int deleteGoods(int id) {

        int value=0;
        //id不为0
        if(id!=0){
            //删除grant表外键goodsid
             grantMapper.deleteByGoodsid(id);
            value=goodsMapper.deleteByGoodsid(id);
        }
        return value;
    }


}




